746C - Tram - CodeForces Solution


constructive algorithms implementation math *1600

Please click on ads to support us..

Python Code:

def solve(a,i,j,m,n,o,d):
    if i>j:
        i=a-i
        j=a-j
        o=a-o
        d*=-1
    tmp=n*(j-i)
    c=0
    if d==-1:
        c=o+i
    elif o<=i:
        c=i-o
    else:
        c=2*a-(o-i)
    tmp=min(tmp,(c+j-i)*m)
    return tmp

a,i,j=map(int,input().split())
m,n=map(int,input().split())
o,d=map(int,input().split())
print(solve(a,i,j,m,n,o,d))

C++ Code:

#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
int main()
{
    double s, x1, x2, t1, t2, p, d;
    cin >> s >> x1 >> x2;
    cin >> t1 >> t2;
    cin >> p >> d;
    double v1 = 1.0 / t1, v2 = 1.0 / t2;
    if (x1 < x2)
    {
        if (v2 >= v1)
        {
            cout << (x2 - x1) * t2 << endl;
        }
        else
        {
            if (d == 1)
            {
                if (p <= x1)
                {
                    cout << min((x2 - p) * t1, (x2 - x1) * t2) << endl;
                }
                else
                {
                    double tx = (s)*t1 + (s - p) * t1;
                    double x1org = x1;
                    x1 = x1 + (tx / t2);
                    if (x1 >= x2)
                    {
                        cout << (x2 - x1org) * t2 << endl;
                    }
                    else
                    {
                        cout << min((x2 - x1) * t2, (x2)*t1) + tx << endl;
                    }
                }
            }
            else
            {
                double tx = (p)*t1;
                double x1org = x1;
                x1 = x1 + (tx / t2);
                if (x1 >= x2)
                {
                    cout << (x2 - x1org) * t2 << endl;
                }
                else
                {
                    cout << min((x2 - x1) * t2, (x2)*t1) + tx << endl;
                }
            }
        }
    }
    else
    { // x2 < x1
        if (v2 >= v1)
        {
            cout << (x1 - x2) * t2 << endl;
        }
        else
        {
            if (d == 1)
            {
                double tx = (s - p) * t1;
                double x1org = x1;
                x1 = x1 - (tx / t2);
                if (x1 <= x2)
                {
                    cout << (x1org - x2) * t2 << endl;
                }
                else
                {
                    cout << min((x1 - x2) * t2, (s - x2) * t1) + tx << endl;
                }
            }
            else
            {
                if (p >= x1)
                {
                    cout << min((x1 - x2) * t2, (p - x2) * t1) << endl;
                }
                else
                {
                    double tx = p * t1 + s * t1;
                    double x1org = x1;
                    x1 = x1 - (tx / t2);
                    if (x1 <= x2)
                    {
                        cout << (x1org - x2) * t2 << endl;
                    }
                    else
                    {
                        cout << min((x1 - x2) * t2, (s - x2) * t1) + tx << endl;
                    }
                }
            }
        }
    }
    return 0;
}


Comments

Submit
0 Comments
More Questions

1606A - AB Balance
1658C - Shinju and the Lost Permutation
1547C - Pair Programming
550A - Two Substrings
797B - Odd sum
1093A - Dice Rolling
1360B - Honest Coach
1399C - Boats Competition
1609C - Complex Market Analysis
1657E - Star MST
1143B - Nirvana
1285A - Mezo Playing Zoma
919B - Perfect Number
894A - QAQ
1551A - Polycarp and Coins
313A - Ilya and Bank Account
1469A - Regular Bracket Sequence
919C - Seat Arrangements
1634A - Reverse and Concatenate
1619C - Wrong Addition
1437A - Marketing Scheme
1473B - String LCM
1374A - Required Remainder
1265E - Beautiful Mirrors
1296A - Array with Odd Sum
1385A - Three Pairwise Maximums
911A - Nearest Minimums
102B - Sum of Digits
707A - Brain's Photos
1331B - Limericks